package com.leyou.item.controller;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.mapper.SpuDetailMapper;
import com.leyou.item.mapper.SpuMapper;
import com.leyou.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class GoodsController {
    @Autowired
  private GoodsService goodsService;
    @GetMapping("spu/page")
    public ResponseEntity<PageResult<SpuDTO>> querySpuByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable", required = false) Boolean saleable,
            @RequestParam(value = "key", required = false) String key
    ){
        return ResponseEntity.ok(goodsService.querySpuByPage(page,rows,saleable,key));
    }

@PostMapping("/goods")
    public ResponseEntity<Void> saveGoods (@RequestBody SpuDTO spuDTO){

        goodsService.saveGoods(spuDTO);
return ResponseEntity.status(HttpStatus.CREATED).body(null);
    }
@PutMapping("/spu/saleable")
    public ResponseEntity<Void> updateSpuSaleable(@RequestParam("id") Long id,@RequestParam("saleable")Boolean saleable){
        goodsService.updateSpuSaleable(id,saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null);
    }


    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailById(@RequestParam("id") Long id){

        return ResponseEntity.ok( goodsService.querySpuDetailById(id));
    }


    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("id") Long id){

        return ResponseEntity.ok( goodsService.querySkuBySpuId(id));
    }

    @PutMapping("/goods")
    public ResponseEntity<Void> updateGoods (@RequestBody SpuDTO spuDTO){

        goodsService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).body(null);
    }

    /**
     * 根据spuid查询spu 、spuDetail、skus,用来封装商品详情页
     */
    @GetMapping("spu/querySpuBySpuId/{id}")
      public   ResponseEntity<SpuDTO> querySpuBySpuId(@PathVariable("id") Long id){
        return ResponseEntity.ok(goodsService.querySpuBySpuId(id));
    }
}
